package com.surprise.base.util;

import java.text.MessageFormat;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import javax.validation.constraints.Min;

import lombok.Data;

import org.hibernate.validator.HibernateValidator;
import org.hibernate.validator.constraints.NotEmpty;
import org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator;
import org.hibernate.validator.resourceloading.ResourceBundleLocator;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;



/**
 * parameter validate
 * @author nick
 * 2013-5-3
 */
public class BeanValidationUtils {
    private static ValidatorFactory vf;
    
    static {
        try {
            vf = Validation
                    .byProvider(HibernateValidator.class)
                    .configure()
                    .messageInterpolator(
                            new ResourceBundleMessageInterpolator(
                                    new ResourceBundleLocator() {
                                        @Override
                                        public ResourceBundle getResourceBundle(
                                                Locale arg0) {
                                            ResourceBundle bundle = ResourceBundle
                                                    .getBundle(
                                                            "ValidationMessages",
                                                            Locale.SIMPLIFIED_CHINESE);
                                            return bundle;
                                        }
                                    })).buildValidatorFactory();
        } catch (Exception e) {
            vf = Validation.buildDefaultValidatorFactory();
        }
    }

    public static <T> List<String> validate(T request) {
        // validator in static{}
        Validator validator = vf.getValidator();
        Set<ConstraintViolation<T>> set = validator.validate(request);
        List<String> errMsgs = Lists.newArrayList();
        for (ConstraintViolation<T> cv : set) {
            String errMsg = MessageFormat.format(
                    "\"key\":\"{0}\",\"value\":\"{1}\",\"msg\":\"{2}\"",
                    cv.getPropertyPath().toString(),
                    cv.getInvalidValue(),
                    cv.getMessage()
            );
            errMsgs.add(errMsg);
        }
        return errMsgs;
    }
    
    public static <T> Map<String, String> validateMap(T request) {
    	Validator validator = vf.getValidator();
        Set<ConstraintViolation<T>> set = validator.validate(request);
        Map<String, String> ret = Maps.newHashMap();
        for (ConstraintViolation<T> cv : set) {
        	String errMsg = MessageFormat.format(
                    "value: {0}, msg:{1}",
                    cv.getInvalidValue(),
                    cv.getMessage()
            );
        	ret.put(cv.getPropertyPath().toString(), errMsg);
        }
        return ret;
    }
    
    /**
     * 
     * @param args
     */
    public static void main(String[] args) {
        Pig pig = new Pig();
        pig.setAge(1);
        pig.setName("123");
        Map<String, String> err = validateMap(pig);
        System.out.println(err);
    }
    
    @Data
    public static class Pig {
        @NotEmpty
        private String name;
        @Min(10)
        private int age;
    }
}
